package in.miraclesirius.arunachaltimes;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.util.Xml.Encoding;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class WordpressFullStoryActivity extends Activity {

	View rootView;
	String Newsstory;
	ProgressDialog mProgressDialog;
	String url;
	final Context context = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_full_story);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		Intent intent = getIntent();
		url = intent.getStringExtra(WordpressMainActivity.FULL_STORY);
		new MainStory().execute();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// Respond to the action bar's Up/Home button
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private class MainStory extends AsyncTask<Void, Void, Void> {

		Elements newsBody;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgressDialog = new ProgressDialog(context);
			mProgressDialog.setMessage("Loading Arunachal times...");
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.show();
			newsBody = new Elements();
		}

		@Override
		protected Void doInBackground(Void... params) {
			try {
				Document document = Jsoup.connect(url).timeout(0).get();
				newsBody = document.select("#content-main")
						.select("[id^=post-]").select("div.entry");
				// Elements removeElement =
				// newsBody.select("p.print, p.meta_categories, div.al2fb_like_button");
				newsBody = newsBody.select("h1.post-title, div.entry-content");

				// newsBody.removeAll(removeElement);
			} catch (IOException e) {
				Log.e("Jsoup: ", "Failed to Connet");
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			WebView content = (WebView) findViewById(R.id.fullStoryWebView);
			content.loadDataWithBaseURL(url, newsBody.toString(), "text/html",
					Encoding.UTF_8.toString(), null);
			WebSettings setings = content.getSettings();

			setings.setTextZoom(300);
			setings.setLoadWithOverviewMode(true);
			setings.setUseWideViewPort(true);
			setings.setBuiltInZoomControls(true);

			mProgressDialog.dismiss();
		}
	}
}
